package model.dao.impl;


import java.sql.Blob;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import util.DataSourceUtil;

import model.beans.CategoryBean;
import model.dao.CategoryDAO;

public class CategoryDAOImp implements CategoryDAO{

	@Override
	public List<CategoryBean> getCategoryList() {
		List<CategoryBean> result = new ArrayList<CategoryBean>();
		try {
			DataSource dataSource = DataSourceUtil.getDataSource();
			Connection conn = dataSource.getConnection();
			Statement statement = conn.createStatement();			
			statement.executeUpdate("Set Schema roumani");
			ResultSet rs= statement.executeQuery("SELECT * FROM CATEGORY");

			while (rs.next()){
				CategoryBean cb = new CategoryBean();
				cb.setId(rs.getInt("ID"));
				cb.setName(rs.getString("NAME"));
				cb.setDescription(rs.getString("DESCRIPTION"));
				Blob image = rs.getBlob("PICTURE");				
				cb.setPicture(image.getBytes(1, (int)image.length()));
				result.add(cb); 

			}
			statement.close();
			conn.close();		
		} catch (Exception e){
			System.out.println("sql error!\n" + e);
		}
		return result;
	}
	

}
